<?php
/**
 * 广告模型
 *
 *@author hary <hary@taomee.com>
 */
class ads_banner_model extends model
{
	private $ad_banner_table;
	private $ad_type_table;
	private $ad_file;

	/**
	 * 构造函数
	 * @return max
	 */
	function __construct()
	{
		parent::__construct();

		$this->ad_banner_table = $this->sys_conf_var[ 'table' ][ 'ad_banner' ];
		$this->ad_type_table   = $this->sys_conf_var[ 'table' ][ 'ad_media_type' ];
		$this->ad_file		   = $this->sys_conf_var['table']['ad_file'];
	}

	/**
	 * 添加一个广告区
	 * @param $ads
	 * @return boolean
	 */
	public function addAdsBanner($banner){

		$banner = $this->formatData($banner);

		if(!is_array($banner)) return false;

		$banner['b_files'] = $banner['b_files'];
		$banner['b_update_date'] = time();

		$this->addDataBatch2($banner,array('b_zone','b_project','b_name','b_start_time','b_end_time','b_mtype','b_files','b_des','b_status','is_along','sort'));

		$result = $this->dataInsert($this->ad_banner_table);

		return $result;
	}

	/**根据id删除ads
	 * @return boolean
	 */
	public function delAdsBannerById($id){

		if(!is_numeric($id)) return false;

		$this->where('b_id',$id,'=','and',0);

		$banner = $this->getDataInfo($this->ad_banner_table);

		$this->where('b_id',$id,'=','and',0);

		return $this->dataDel($this->ad_banner_table);
	}

	/**
	 * 根据id获取获取ads
	 * @return max
	 */
	public function getAdsBannerById($id){

		if(!is_numeric($id)) return false;
			
		$this->where('b_id',$id,'=','and',0);
			
		$result = $this->getDataInfo($this->ad_banner_table);

		return $this->casecadeBanner($result);
	}
	
	/**
	 * 点击率添加
	 * @param $file 点击率保存在file json数据中的sort中
	 * @return unknown_type
	 */
	public function saveBannerHit($file){
		if(is_array($file))	$file = json_encode($file);
		
		$this->where("b_files",$file);
		
	}

	/**
	 * 根据zone的key获取banner
	 * @return unknown_type
	 */
	public function getAdsBannerByZoneKey($key){

		$this->loadModel("ads_zone_model");
		$zone = $this->ads_zone_model->getAdsZoneByKey($key);

		return $this->getBannerByZone($zone['z_id']);
	}

	/**
	 * 更改ads项
	 * @return boolean
	 */
	public function changeAdsBannerById($banner){

		$banner = $this->formatData($banner);

		if(!is_array($banner)) return false;

		$id = intval($banner['b_id']);

		$banner['b_files'] = $banner['b_files'];
		$banner['b_update_date'] = time();

		$this->addDataBatch2($banner,array('b_zone','b_project','b_name','b_start_time','b_end_time','b_mtype','b_files','b_des','b_status','is_along','sort'));

		$this->where('b_id',$id);

		return $this->dataUpdate($this->ad_banner_table);

	}

	/**
	 * 根据关键字获取多个ads
	 * @return max
	 */
	public function getAdsBannersByKeywords($keywords,$page='1',$page_size='10',$order='sort desc'){

		$keywords = $this->formatData($keywords);

		if(empty($keywords))
		{
			$sql = "select * from $this->ad_banner_table ";
		}
		else
		{
			$sql = "select * from $this->ad_banner_table where
	   				b_name like '%$keywords%' or 
	   				b_des   like '%$keywords%'";
		}

		if($page) $this->setPage($page);

		if($page_size) $this->setLimit($page_size);

		if($order) $this->setOrder('order by '.$order);

		return $this->customQuery($sql);
	}

	/**
	 * 根据project id获取banner
	 * @param $id
	 * @return array
	 */
	public function getAdsBannersByPro($id){

		$id = $this->formatData($id);

		$this->where("b_pro",$id,'=','and',0);

		return $this->getDataList($this->ad_banner_table);
	}

	/**
	 * 查询某个广告位是否有在线广告发布
	 * @return boolean
	 */
	public function hasRelease($zoneid){

		$zoneid = $this->formatData($zoneid);

		$this->where('b_zone',$zoneid);

		$this->where('b_status',1,'=','and',1);

		$banners = $this->getDataList($this->ad_banner_table);

		if(empty($banners))
		{
			return false;
		}
		else
		{
			return $banners;
		}
	}

	/**
	 * 更改状态
	 * @param $bid
	 * @param $status
	 * @return unknown_type
	 */
	public function changeStatus($bid,$status)
	{
		$bid = $this->formatData($bid);

		$this->where('b_id',$bid);

		$this->addData('b_status',$status);

		return $this->dataUpdate($this->ad_banner_table);

	}

	/**
	 * 根据广告去获取banner
	 * @return array
	 */
	public function getBannerByZone($zoneid){

		$zoneid = $this->formatData($zoneid);

		$this->where('b_zone',$zoneid,'=','and',1);

		$this->where("b_status",1,'=','and',1);

		$result = $this->getDataInfo($this->ad_banner_table);
		
		return $this->casecadeBanner($result);
	}
	
	/**
	 * 根据广告为获取广告s
	 * @return unknown_type
	 */
	public function getBannersByZone($zoneid){
		
		$zoneid = $this->formatData($zoneid);
		
		$this->where('b_zone',$zoneid);
		
		return $this->getDataList($this->ad_banner_table);
		
	}

	/**
	 * banner的相关属性级联操作
	 * @param $result
	 * @return unknown_type
	 */
	private function casecadeBanner($banner){

		if(!$banner) return false;
			
		$this->loadModel("ads_modal_model");
		$mtype = $this->ads_modal_model->getModalById($banner['b_mtype']);
		$banner['b_mtype'] = $mtype;
			
		$banner['b_files'] = $this->toArray(json_decode($banner['b_files']));
			
		$this->loadModel("ads_zone_model");
		$banner['zone'] = $this->ads_zone_model->getAdsZoneById($banner['b_zone']);
			
		return $banner;

	}


}